var BusCon = require("../../core/bus/_con");
var db = require("../../core/db/index");
var config = require("../../core/_include/config");
var ResponObj = require("../_include/responObj");
var BuffCon = require("../../core/buff/_con");
var myfs = require("../_include/fs");
var theme = require("./theme");
var role = require("./role");

module.exports = {
	prefix: "/bus/card_factory",
	get: {
		"/list": function() {
			var bus = role.getBus(this);
			var query = this.request.query;
			this.body = ResponObj("json", bus.getCardFactoryList(query.num, query.page));
		},
		"/goods_list_with_page_info/:card_factory_id": function() {
			var bus = role.getBus(this);
			var query = this.request.query;

			var card_factory_id = this.params.card_factory_id;
			this.body = ResponObj("json", bus.getGoodsListFromCardFactory_PageInfo(card_factory_id, query.num, query.page));
		}
	},
	post: {
		"/add_card_factory": function() {
			var bus = role.getBus(this);
			var card_factory_info = this.request.body;
			this.body = ResponObj("json", bus.addCardFactory(card_factory_info));
		},
		"/add_goods_to_card_factory/:card_factory_id": function() {
			var bus = role.getBus(this);
			var goods_info = this.request.body;
			var card_factory_id = this.params.card_factory_id;
			this.body = ResponObj("json", bus.addGoodsToCardFactory(card_factory_id, goods_info, this.request.query.num || this.request.body.num || 1));
		},
	},
	put: {
		"/update_card_factory/:card_factory_id": function() {
			var bus = role.getBus(this);
			var card_factory_id = this.params.card_factory_id;
			var card_factory_info = this.request.body.info;
			this.body = ResponObj("json", bus.updateCardFactory(card_factory_id, card_factory_info));
		},
		"/update_goods_from_card_factory/:card_factory_id": function() {
			var bus = role.getBus(this);
			var card_factory_id = this.params.card_factory_id;
			var goods_info = this.request.body;
			this.body = ResponObj("json", bus.updateGoodsFromCardFactory(card_factory_id, goods_info._id, goods_info));
		},
		"/update_goods_index_from_card_factory/:card_factory_id": function() {
			var bus = role.getBus(this);
			var card_factory_id = this.params.card_factory_id;
			var body = this.request.body;
			this.body = ResponObj("json", bus.updateGoodsIndexFromCardFactory(card_factory_id, body.goods_id, body.goods_index));
		}
	},
	"delete": {
		"/remove_card_factory/:card_factory_id": function() {
			var bus = role.getBus(this);
			var card_factory_id = this.params.card_factory_id;
			var body = this.request.body;
			bus.removeCardFactory(card_factory_id);
			this.body = ResponObj("json", bus.getCardFactoryList(body.num, body.page));
		},
		"/remove_goods_from_card_factory/:card_factory_id": function() {
			var bus = role.getBus(this);
			var card_factory_id = this.params.card_factory_id;
			var body = this.request.body;
			if (!body.goods_id) {
				throw "参数错误"
			}
			bus.removeGoodsFromCardFactory(card_factory_id, body.goods_id);
			this.body = ResponObj("json", bus.getGoodsListFromCardFactory_PageInfo(card_factory_id, body.num, body.page));
		}
	}
}